import ImportRegion from './ImportRegion';
import ITool from '../ITool';
import Prefix from '../../constant/Prefix';
import Region from './Region';
import RegionBorder from '../../../../constant/RegionBorder';
import RegionTree from './RegionTree';

/**
 * 区域划分工具箱
 * @implements {ITool}
 */
class RegionTool extends ITool {
  constructor(_top, _impl) {
    super(_top, _impl);
    this.name = 'RegionTool';
    this.desc = '区域绘制工具箱';
    this.layeridPrefix = Prefix['RegionTool'];
    this.qyflType = { city: 2, fq: 3, wg: 4, dy: 5 };
    this.edgeColor = RegionBorder['Default'];
    this.changeType = '';
    this.changeQyfl = '';
    this.instance = {};
    this.importRegion = null;
    this.regionTree = null;
    this.totalFeatures = [];
  }

  onAdd() {
    super.onAdd();
    Object.keys(this.qyflType).forEach((key) => {
      let qyfl = this.qyflType[key];
      this.instance[qyfl] = new Region(this, qyfl);
    });
    this.regionTree = new RegionTree(this);
    this.importRegion = new ImportRegion(this);
  }

  onRemove() {
    super.onRemove();
  }

  /**
   * 切换区域边界式样
   * @param {String} style 式样名称
   */
  changeBorder(style) {
    this.edgeColor = RegionBorder[style];
    Object.keys(this.instance).forEach((key) => {
      this.instance[key].refresh();
    });
    this.regionTree.refresh();
    this.importRegion.refresh();
  }

  /**
   * 加载区域
   * @param {features} features 区域图形数据
   */
  render(features) {
    this.totalFeatures = features;
    let regionMap = {};
    features.forEach((feature) => {
      let qyfl = feature.properties.qyfl;
      let style = this.getStyleByQyfl(qyfl);
      feature.properties = Object.assign(feature.properties, style);
      if (regionMap[qyfl]) {
        regionMap[qyfl].push(feature);
      } else {
        regionMap[qyfl] = [feature];
      }
    });
    Object.keys(this.instance).forEach((key) => {
      this.instance[key].render(regionMap[key] || []);
    });
    if (this.totalFeatures && this.totalFeatures.length > 0) {
      let polygon = turf.envelope(turf.featureCollection(this.totalFeatures));
      this.getTool('BaseMapTool').fitBounds(turf.bbox(polygon));
    }
  }

  /**
   * 加载区域树形结构
   * @param {Array} datas 区域图形数据
   * @param {*} events 区域事件
   * @param {*} defaultSelected 默认选中区域
   */
  renderRegionTree(datas, events, defaultSelected) {
    this.regionTree.render(datas, events, defaultSelected);
  }

  /**
   * 绘制区域
   * @param {Number} qyfl 区域类型
   * @param {*} events 绘制事件
   */
  draw(qyfl, events) {
    this.changeType = 'add';
    this.changeQyfl = qyfl;
    this.reset();
    setTimeout(() => {
      this.instance[qyfl].draw(events);
    }, 100);
  }

  /**
   * 编辑区域
   * @param {Number} qyfl 区域类型
   * @param {*} events 绘制事件
   */
  edit(qyfl, events) {
    this.changeType = 'edit';
    this.changeQyfl = qyfl;
    this.reset();
    setTimeout(() => {
      this.instance[qyfl].edit(events);
    }, 100);
  }

  /**
   * 分割区域
   * @param {Number} qyfl 区域类型
   */
  split(qyfl) {
    return new Promise((resolve) => {
      this.instance[qyfl].split().then((result) => {
        resolve(result);
      }).catch(() => { });
    });
  }

  /**
   * 清空区域
   */
  clear() {
    Object.keys(this.instance).forEach((key) => {
      this.instance[key].clear();
    });
  }

  /**
   * 重置区域
   */
  reset() {
    this.changeType = '';
    this.changeQyfl = '';
    Object.keys(this.instance).forEach((key) => {
      this.instance[key].reset();
    });
  }

  /**
   * 选择文件
   * @param {*} events 回调事件
   */
  selectFile(events) {
    this.importRegion.selectFile((e) => {
      events.onChangeFile(e);
    });
  }

  /**
   * 上传已选择的文件
   * @param {Object} option 参数
   */
  async doUpload(option) {
    let result = await this.importRegion.doUpload(option);
    return result;
  }

  /**
   * 上传用户自己的文件
   * @param {File} file 文件
   * @param {Object} option 参数
   */
  async uploadFile(file, option) {
    let result = await this.importBlock.uploadFile(file, option);
    return result;
  }

  /**
   * 清除导入图层
   */
  clearImported() {
    this.importRegion.clear();
  }

  /**
   * 切换图层显示状态
   * @param {Array} visibleLayers 显示图层
   */
  changeLayerVisible(visibleLayers) {
    this.importRegion.changeLayerVisible(visibleLayers);
  }

  /**
   * 切换图层类型
   * @param {Array} layers 改变图层
   * @param {Number} qyfl 区域类型
   */
  changeLayerType(layers, qyfl) {
    this.importRegion.changeLayerType(layers, qyfl);
  }

  /**
   * 根据区域类型生成颜色
   * @param {Number} qyfl 区域类型
   */
  getColorByQyfl(qyfl) {
    return this.edgeColor[qyfl].color;
  }

  /**
   * 根据区域类型生成式样
   * @param {Number} qyfl 区域类型
   */
  getStyleByQyfl(qyfl) {
    let style = this.edgeColor[qyfl];
    return {
      opacity: 0,
      'line-width': style.width,
      'line-color': style.color,
      'line-opacity': 1
    };
  }
}

export default RegionTool;
